package com.briup.mt.service.impl;

import com.briup.mt.bean.Module;
import com.briup.mt.bean.ModuleExample;
import com.briup.mt.dao.ModuleMapper;
import com.briup.mt.service.IModuleService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ModuleImpl implements IModuleService {

    @Resource
    private ModuleMapper moduleMapper;

    @Override
    public List<Module> query(String name, String status) {
        ModuleExample example = new ModuleExample();
        if(!StringUtils.isEmpty(name)){
            example.createCriteria().andNameLike("%" + name + "%");
        }
        if(!StringUtils.isEmpty(status)){
            example.createCriteria().andStatusEqualTo(status);
        }
        List<Module> list = moduleMapper.selectByExample(example);
        return list;
    }

    @Override
    public void saveOrUpdate(Module module) {
        Long id = module.getId();
        if(moduleMapper.selectByPrimaryKey(id) == null){
            moduleMapper.insert(module);
        }else {
            moduleMapper.updateByPrimaryKey(module);
        }
    }

    @Override
    public void deleteById(Long id) {
        moduleMapper.deleteByPrimaryKey(id);
    }
}
